package com.heima.test6;

import java.util.concurrent.ArrayBlockingQueue;

class MyRun1 implements Runnable {
    public ArrayBlockingQueue q;

    public MyRun1(ArrayBlockingQueue q) {
        this.q = q;
    }

    @Override
    public void run() {
        //向队列里添加数据
        for (int i = 0; i < 10000; i++) {

            try {
                q.put(i);//存入队列 队列满了 代码就停在这里
                System.out.println("存入了" + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyRun2 implements Runnable {
    public ArrayBlockingQueue q;

    public MyRun2(ArrayBlockingQueue q) {
        this.q = q;
    }

    @Override
    public void run() {
        while (true) {

            try {
                System.out.println("从队列里取数据");
                int peek = (int) q.take();//从队列里取数据 如果没有数据 就停在这里
                System.out.println(peek);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Demo03 {
    public static void main(String[] args) {
        ArrayBlockingQueue q = new ArrayBlockingQueue(10);

        MyRun1 m1 = new MyRun1(q);
        MyRun2 m2 = new MyRun2(q);

        new Thread(m1).start();
        new Thread(m2).start();
    }
}
